package com.wimcorp.magic.cost;

import java.rmi.RemoteException;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

import com.wimcorp.magic.board.CardAccessor;
import com.wimcorp.magic.board.Graveyard;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.cards.PlayingCard;
import com.wimcorp.magic.exception.CostException;
import com.wimcorp.magic.input.Location;
import com.wimcorp.magic.input.Target;
import com.wimcorp.magic.input.Type;

@Entity
public class ReturnCost extends Cost {

	@Enumerated(EnumType.STRING)
	private Type type;

	@Enumerated(EnumType.STRING)
	private Location location;

	@Enumerated(EnumType.STRING)
	private Location destination;

	protected ReturnCost() {
	}

	public ReturnCost(Type type, Location location, Location destination) {
		this.type = type;
		this.location = location;
		this.destination = destination;
	}

	public ReturnCost(ReturnCost cost) {
		super(cost);
		this.type = cost.getType();
		this.location = cost.getLocation();
		this.destination = cost.getDestination();
	}

	public void payCost() throws CostException{
		try {
			long id = getPlayer().getUserInterface().select(Target.SELF, type, location);

			PlayingCard card = (Creature) CardAccessor.getCardAccessor(getPlayer(), Location.BATTLEFIELD.toString())
					.getCard(id);
			switch (destination) {
			case BOTTOM_DECK:
				card.unsummon();
				getPlayer().getDeck().addOnBottom(card);
				break;
			case EXILE:
				card.exile();
				break;
			case GRAVEYARD:
				card.graveyard();
				break;
			case HAND:
				card.unsummon();
				getPlayer().getHand().add(card);
				break;
			case TOP_DECK:
				card.unsummon();
				getPlayer().getDeck().addOnTop(card);
				break;
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public String toString() {
		return "Return card from " + location + " to " + destination;

	}

	@Override
	public ReturnCost clone() {
		return new ReturnCost(this);
	}

	public Type getType() {
		return type;
	}

	public Location getLocation() {
		return location;
	}

	public Location getDestination() {
		return destination;
	}

}
